﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AimGameLibrary.GameObject
{
    public class GameTakeCamera : GameVector2
    {
        float speed;
        public Vector2 OriginScreen;
        public Texture2D texture2d;
        Rectangle Bounder;
        Rectangle Area;
        Boolean ShowDebug;

        public SpriteBatch spritebatch;
        public Camera camera;
        
        public GameTakeCamera()
        {
            OriginScreen = Vector2.Zero;
            Bounder = Rectangle.Empty;
            Area = Rectangle.Empty;
        }

        public override void LoadData(Identification getdata, int IndexData, NodeData node)
        {
            InputMode = (NodeInputMode)getdata.Items.GetValue(IndexData + 1);
            speed = (float)getdata.Items.GetValue(IndexData + 2);
            Bounder = (Rectangle)getdata.Items.GetValue(IndexData + 3);
            Area = (Rectangle)getdata.Items.GetValue(IndexData + 4);
            ShowDebug = (Boolean)getdata.Items.GetValue(IndexData + 5);
        }

        public override void Draw()
        {
            if (texture2d != null && spritebatch != null && camera != null)
            {
                Rectangle size = new Rectangle(0, 0, (int)OriginScreen.X * 2,(int)OriginScreen.Y * 2);
                Color main = new Color(Color.Green.R,Color.Green.G,Color.Green.B,(byte)60);
                spritebatch.Draw(texture2d, camera.ApplyPosition(myvector2), size, main,0f,Vector2.Zero,1f,SpriteEffects.None,0f);
            }
        }

        public override void UpdateTime(TimeSpan timespan)
        {
            base.UpdateTime(timespan);
            if (Listparent.Count == 1)
            {
                Logic();
            }
        }

        public override void UpdateGameTime(Microsoft.Xna.Framework.GameTime gametime)
        {
            base.UpdateGameTime(gametime);
            
            if (Listparent.Count == 1)
            {
                Logic();
            }
        }

        void Logic()
        {
            Vector2 tempPosition = Vector2.Zero;
            if (Listparent[0].GetType() == typeof(GameVector2))
            {
                GameVector2 tempVector2Target = (GameVector2)Listparent[0];
                tempPosition = tempVector2Target.Myvector2 - OriginScreen;
            }
            else if (Listparent[0].GetType() == typeof(GameBody))
            {
                GameBody tempBodyTarget = (GameBody)Listparent[0];
                tempPosition = new Vector2(tempBodyTarget.MybodY.X, tempBodyTarget.MybodY.Y) - OriginScreen;
            }
            myvector2 = DetactionBouder(myvector2, tempPosition, Bounder);
            myvector2 = Vector2.Lerp(myvector2, tempPosition, speed);
            myvector2 = DetactionArea(myvector2, Area);
            //* gametime.TotalRealTime.Milliseconds / 1000.0f
        }

        Vector2 DetactionArea(Vector2 MyPosition, Rectangle DetectionArea)
        {
            if (MyPosition.X < DetectionArea.X)
                MyPosition.X = DetectionArea.X;
            if (MyPosition.X > DetectionArea.Right)
                MyPosition.X = DetectionArea.Right;

            if (MyPosition.Y < DetectionArea.Y)
                MyPosition.Y = DetectionArea.Y;
            if (MyPosition.Y > DetectionArea.Bottom)
                MyPosition.Y = DetectionArea.Bottom;
            return MyPosition;
        }
        Vector2 DetactionBouder(Vector2 MyPosition, Vector2 TargetPosition,Rectangle DetectionBounder)
        {
            // Left
            if (TargetPosition.X - MyPosition.X < DetectionBounder.Width)
                MyPosition.X = TargetPosition.X - DetectionBounder.Width;

            // Right
            if (TargetPosition.X - MyPosition.X > DetectionBounder.Height)
                MyPosition.X = TargetPosition.X - DetectionBounder.Height;

            // Top
            if (TargetPosition.Y - MyPosition.Y > DetectionBounder.X)
                MyPosition.Y = TargetPosition.Y - DetectionBounder.X;

            // Bottom
            if (TargetPosition.Y - MyPosition.Y < DetectionBounder.Y)
                MyPosition.Y = TargetPosition.Y - DetectionBounder.Y;

            return MyPosition;
        }
    }
}
